package org.chench.test.helloservice.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Enumeration;

@RestController
public class SayHelloController {
    @Value("${server.port}")
    private String serverPort;

    @GetMapping("/hello/{name}")
    public String sayHello(HttpServletRequest req, HttpServletResponse resp,
                        @PathVariable("name") String name) {
        // 读取HTTP消息头
//        System.out.println("==========打印HTTP消息头开始====================");
//        Enumeration<String> names = req.getHeaderNames();
//        while(names.hasMoreElements()) {
//            String hn = names.nextElement();
//            System.out.println(hn + ": " + req.getHeader(hn));
//        }
//        System.out.println("==========打印HTTP消息头结束====================");


        // 让服务端生成Session
        req.getSession(true);
//        Cookie cookie = new Cookie("JSESSIONID", req.getSession().getId());
//        resp.addCookie(cookie);

        return new StringBuilder()
                .append("Hello, ").append(name).append("! I am from: ").append(serverPort)
                .toString();
    }
}
